package main.com.oaer.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import main.com.oaer.model.Role;
import main.com.oaer.service.RoleService;
import main.com.oaer.util.CommonUtil;

/**
 * 角色控制器
 *
 */
@Controller
@RequestMapping("/role")
public class RoleAction {
	
	@Autowired
	RoleService roleService;	
	
	/**
	 * 进入角色列表
	 * @param modelMap  模型map
	 * @return   "system/listRole"  角色列表
	 */
	@RequestMapping(value="/toList")
	public String entryList(ModelMap modelMap){
		List<Role> rolesList = roleService.findRoles();
		modelMap.put("rolesList", rolesList);
		return "system/listRole";
	}
	
	/**
	 * 增加一个角色，以json格式异步返回消息
	 * @param response  回应对象
	 * @param role 待插入角色对象
	 */
	@RequestMapping(value="/add")
	public void add(HttpServletResponse response, Role role){
		roleService.add(role);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("status", "success");
		CommonUtil.printToHTML(response, map);
	}

	/**
	 * 删除角色
	 * @param ids  角色id连接字符串，如：1,2,3,4,5 或 简单如：9
	 */
	@RequestMapping(value="/delete")
	public void delete(HttpServletResponse response, String ids){
		Map<String,Object> map = new HashMap<String,Object>();
		if(ids!=null){
			Long[] lIds = CommonUtil.stringToLong(ids.split(","));
			for(Long lId:lIds){
				roleService.delete(roleService.load(lId));
			}
			map.put("status", "success");
			CommonUtil.printToHTML(response, map);
		}else{
			map.put("status", "error");
			CommonUtil.printToHTML(response, map);
		}
	}
}
